package eerr.llrr.oorr.pte9983.b.utils

import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.text.TextUtils
import android.util.Log
import com.tencent.mmkv.MMKV
import java.lang.Exception
import java.util.UUID

object survivalUtils {

    const val LIVE_START = 1
    var lastTime:Long = 0

    var handler:Handler = object :Handler(Looper.myLooper()!!){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)

            val what = msg.what

            if (LIVE_START == what){
                var flag = msg.arg1
                if (flag == 1){
                    val firstInTime:Long = MMKV.defaultMMKV().decodeLong("live:time",0)
                    if (firstInTime <= 0){
                        var uid = MMKV.defaultMMKV().decodeString("uuid:user")
                        if (TextUtils.isEmpty(uid)){
                            uid = UUID.randomUUID().toString()
                            MMKV.defaultMMKV().encode("uuid:user",uid)
                        }
                    }

                    lastTime = System.currentTimeMillis()

                } else{
                    val liTimeGap = System.currentTimeMillis() - lastTime
                    var liTotaTime = MMKV.defaultMMKV().decodeLong("live:time",0)
                    liTotaTime += liTimeGap

                    MMKV.defaultMMKV().encode("live:time",liTotaTime)
                    lastTime = System.currentTimeMillis()
                }

                val obtain = Message.obtain()
                obtain.what = LIVE_START
                obtain.arg1 = ++flag
                // 每隔30s 打一个点

                if (isNeedReport()){
                    handleReport()
                }
                this.sendMessageDelayed(obtain,30000)
            }
        }
    }

    fun startLvMsg(){

        val message = Message.obtain()
        message.what = LIVE_START
        message.arg1 = 1;
        handler.sendMessageDelayed(message,5000)

    }




    fun  handleReport(){
        val liveTotaTime:Long = MMKV.defaultMMKV().decodeLong("live:time",0)
        if (liveTotaTime <= 0){
            return
        }

        val params = HashMap<String,Any?>()
        params["time"] = liveTotaTime
        val uuid = MMKV.defaultMMKV().decodeString("uuid:user")
        params["uuid"] = uuid
        params["os_sdk_version"] = "android_"+ Build.VERSION.SDK_INT


        yyee.llee.mmee.pl190.host668.NetManager.getInstance().doLivePost(params,object :
            yyee.llee.mmee.base_api_net.MCallback {
            override fun onSuccess(response: String?) {
                Log.d("pppppp","doLivePost >>onSuccess》》")

                MMKV.defaultMMKV().encode("live:time:lastreport",System.currentTimeMillis())
                MMKV.defaultMMKV().encode("uuid:user",UUID.randomUUID().toString())
                MMKV.defaultMMKV().encode("live:time",0)


            }

            override fun onError(e: Exception?) {
            }

        })

    }





    fun isNeedReport():Boolean{
        val lastReport = MMKV.defaultMMKV().decodeLong("live:time:lastreport",0)
        if (lastReport <= 0){
            MMKV.defaultMMKV().encode("live:time:lastreport",System.currentTimeMillis())
            return false
        }

        val l = System.currentTimeMillis() - lastReport
        Log.d("pppppp","isNeedReport》》"+l)
        if (l > 0 && l >= 60*1000*10){
            return true
        }

        return false

    }


}